// 圈子接口
const express = require("express");
const route = express.Router();
const pool = require("../utils/mysql");
// 引入验证规则
const Joi = require('joi')


// 获取所有帖子
route.get('/circle/blogs', (req, res) => {
    let { pno, pagesize } = req.query
    // 表单验证
    let schema = Joi.object({
        pno: Joi.string().required(), // 必填
        pagesize: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }

    let startIndex = (pno - 1) * pagesize
    let sql = "select * from pet_blogs left join pet_user on pet_blogs.u_id=pet_user.u_id order by b_id desc LIMIT ?,?;SELECT count(*) as total FROM pet_blogs"
    pool.query(sql, [parseInt(startIndex), parseInt(pagesize)], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            let [blogs, total] = r
            total = total[0].total
            res.send({ code: 200, data: { blogs, total, pno, pagesize } })
        }
    })
})

// 通过id查询帖子
route.get('/circle/blogs_id', (req, res) => {
    let { b_id } = req.query
    let schema = Joi.object({
        b_id: Joi.string().required()
    })
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }

    let sql = 'select * from pet_blogs where b_id=?'
    pool.query(sql, [b_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, data: r })
        }

    })
})

// 发布帖子
route.post('/circle/blogs_add', (req, res) => {

    let { u_id, b_img, b_content } = req.body
    let schema = Joi.object({
        u_id: Joi.string().required(), // 必填
        b_img: Joi.string().required(), // 必填
        b_content: Joi.string().required() // 必填
    });
    let { error } = schema.validate(req.body);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }
    let sql = "insert into pet_blogs(b_id,u_id,b_img,b_content) values(null,?,?,?)"
    pool.query(sql, [u_id, b_img, b_content], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, msg: 'ok' })
        }
    })
})

// 删除帖子
route.delete('/circle/blogs_del', (req, res) => {
    let { b_id } = req.query

    let schema = Joi.object({
        b_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }

    let sql = "delete from pet_blogs where b_id= ?"
    pool.query(sql, [b_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, msg: 'ok' })
        }
    })
})

// 获取所有评论
route.get('/circle/blogs_comment', (req, res) => {
    let { b_id } = req.query
    let schema = Joi.object({
        b_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }

    let sql = "SELECT u.u_id, c_id,c_comment,u_name,u_img FROM(pet_comment c LEFT JOIN pet_user u ON c.u_id = u.u_id)right JOIN pet_blogs b ON c.b_id = b.b_id WHERE b.b_id=? order by c_id desc"
    pool.query(sql, [b_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, data: r })
        }
    })
})

// 删除评论
route.delete('/circle/blogs_comment_del', (req, res) => {
    let { c_id } = req.query

    let schema = Joi.object({
        c_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }



    let sql = "delete from pet_comment where c_id=?"
    pool.query(sql, [c_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, msg: 'ok' })
        }
    })
})
// 写入评论
route.post('/circle/blogs_comment_add', (req, res) => {
    let { u_id, b_id, c_comment } = req.body

    let schema = Joi.object({
        u_id: Joi.string().required(), // 必填
        b_id: Joi.string().required(), // 必填
        c_comment: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.body);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }

    let sql = "insert into pet_comment(c_id,u_id,b_id,c_comment) values (null,?,?,?)"
    pool.query(sql, [u_id, b_id, c_comment], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, msg: 'ok' })
        }
    })
})

// 获取帖子点赞数
route.get('/circle/pet_blogs_d', (req, res) => {

    let { b_id } = req.query

    let schema = Joi.object({
        b_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }


    let sql = "select count(*) as count from pet_blogs_d where b_id=?"
    pool.query(sql, [b_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, data: r })
        }
    })
})

// 给帖子点赞
route.post('/circle/pet_blogs_d_add', (req, res) => {
    let { b_id } = req.body

    let schema = Joi.object({
        b_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.body);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }

    let sql = "insert into pet_blogs_d(d_id,dboolern,b_id) values(null,'true',?)"
    pool.query(sql, [b_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, msg: 'ok' })
        }
    })
})
// 取消点赞
route.delete('/circle/pet_blogs_d_del', (req, res) => {
    let { d_id } = req.query

    let schema = Joi.object({
        d_id: Joi.string().required(), // 必填
    });
    let { error } = schema.validate(req.query);
    if (error) {
        res.send({ code: 401, err: error.details });
        return; // 结束
    }

    let sql = "delete from pet_blogs_d where d_id=?"
    pool.query(sql, [d_id], (err, r) => {
        if (err) {
            res.send({ code: 500, msg: err })
        } else {
            res.send({ code: 200, msg: 'ok' })
        }
    })
})

module.exports = route